package exam.huawei.S2;

import java.util.*;

/**
 * 注意点：
 *  1、使用dfs寻找环，需要用path记录路径，但是最终的环是要去除入环前面的路径的，比如路径是-o，要把-去掉
 *  2、每个节点的编号不一定是从1顺序开始的，可能是1 3 9这样，所以需要用两个int[]来记录编号和序号之间的映射关系
 *  3、打印环的时候，需要从最小编号开始打印，所以要根据编号进行排序
 *  4、如果寻找到环，那么dfs就可以提前退出，剪枝
 *  5、由于可能并不是一个连通图，所以要以每个点作为起点进行一次dfs
 */
public class Main {

    public static List<Integer>[] graph = new List[10000];
    public static int[] mapid2num = new int[10000];
    public static int[] mapnum2id = new int[10000];
    public static int cnt;
    public static List<Integer> res;

    static {
        Arrays.fill(mapnum2id, -1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int idx = 0;
        for (int i = 0; i < n; i++) {
            int m = sc.nextInt();
            int a = sc.nextInt();
            if (mapnum2id[a] < 0) {
                mapnum2id[a] = idx;
                mapid2num[idx++] = a;
            }
            int aid = mapnum2id[a];
            if (graph[aid] == null)
                graph[aid] = new ArrayList<>();
            for (int j = 0; j < m-1; j++) {
                int b = sc.nextInt();
                if (mapnum2id[b] < 0) {
                    mapnum2id[b] = idx;
                    mapid2num[idx++] = b;
                }
                int bid = mapnum2id[b];
                graph[aid].add(bid);
            }
        }
        cnt = idx;
        solution();
    }

    private static void solution() {
        List<Integer> path = new ArrayList<>();
        boolean[] visited = new boolean[cnt];
        for (int i = 0; i < cnt; i++) {
            if (res == null)
                dfs(i, path, visited);
            else
                break;
        }
        res.sort(Comparator.comparingInt(a -> mapid2num[a]));
        for (int i=0; i<res.size(); ++i)
            System.out.print(mapid2num[res.get(i)] + " ");
        System.out.println(mapid2num[res.get(0)]);
    }

    private static void dfs(int cur, List<Integer> path, boolean[] visited) {
        if (res != null)
            return;
        if (visited[cur]) {
            res = new ArrayList<>();
            boolean flag = false;
            for (int i = 0; i < path.size(); i++) {
                if (!flag && path.get(i) == cur)
                    flag = true;
                if (flag)
                    res.add(path.get(i));
            }
            return;
        }
        if (graph[cur] == null || graph[cur].size() == 0)
            return;
        visited[cur] = true;
        path.add(cur);
        for (int i = 0; i < graph[cur].size(); i++)
            dfs(graph[cur].get(i), path, visited);
        path.remove(path.size()-1);
        visited[cur] = false;
    }
}
